using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class WinPanel : MonoBehaviour
{
	private float starFillMaxHeight;

	private RectTransform star1Fill;

	private RectTransform star2Fill;

	private RectTransform star3Fill;

	private RectTransform crownFill;

	private Image crownFillImage;

	private bool showStar1;

	private bool showStar2;

	private bool showStar3;

	private bool showCrown;

	private float timeBeforeStar1Animation;

	private float timeBeforeStar2Animation;

	private float timeBeforeStar3Animation;

	private float timeBeforeCrownAnmation = 0.6f;

	private float speedStar1Animation = 0.15f;

	private float speedStar2Animation = 0.15f;

	private float speedStar3Animation = 0.15f;

	private float speedCrownAnimation = 0.15f;

	private float star1MinSize = 0.85f;

	private float star1MaxSize = 1f;

	private float star2MinSize = 1f;

	private float star2MaxSize = 1.15f;

	private float star3MinSize = 0.85f;

	private float star3MaxSize = 1f;

	private float speedResizeStar = 0.2f;

	private float crownSpeedAlpha = 0.2f;

	public GameObject starParticlesPrefab;

	private Transform glassPosition;

	private GameObject instanceDrink;

	private float drinkScale = 30f;

	private void Awake()
	{
		star1Fill = base.transform.FindDeepChild("Star1").FindDeepChild("Star_Fill").GetComponent<RectTransform>();
		star2Fill = base.transform.FindDeepChild("Star2").FindDeepChild("Star_Fill").GetComponent<RectTransform>();
		star3Fill = base.transform.FindDeepChild("Star3").FindDeepChild("Star_Fill").GetComponent<RectTransform>();
		crownFill = base.transform.FindDeepChild("Crown").FindDeepChild("Crown_Fill").GetComponent<RectTransform>();
		starFillMaxHeight = star1Fill.GetComponent<Image>().sprite.rect.height;
		RectTransform rectTransform = star1Fill;
		Vector2 sizeDelta = star1Fill.sizeDelta;
		rectTransform.sizeDelta = new Vector3(sizeDelta.x, 0f);
		RectTransform rectTransform2 = star2Fill;
		Vector2 sizeDelta2 = star2Fill.sizeDelta;
		rectTransform2.sizeDelta = new Vector3(sizeDelta2.x, 0f);
		RectTransform rectTransform3 = star3Fill;
		Vector2 sizeDelta3 = star3Fill.sizeDelta;
		rectTransform3.sizeDelta = new Vector3(sizeDelta3.x, 0f);
		crownFillImage = crownFill.GetComponent<Image>();
		Image image = crownFillImage;
		Color color = crownFillImage.color;
		float r = color.r;
		Color color2 = crownFillImage.color;
		float g = color2.g;
		Color color3 = crownFillImage.color;
		image.color = new Color(r, g, color3.b, 0f);
		glassPosition = base.transform.FindDeepChild("Glass");
	}

	private void Start()
	{
		AudioController.instance.PauseBackground();
		AudioController.instance.Play("winning");
		GameEndPermissions.IncreaseCompletedGamesNumber();
		GameEndPermissions.ShowSpecialPanel(isWin: true);
	}

	private void PlayStarPartciles(Transform position)
	{
		GameObject gameObject = UnityEngine.Object.Instantiate(starParticlesPrefab, position);
		ParticleSystem component = gameObject.GetComponent<ParticleSystem>();
		component.Play();
		ParticleSystem component2 = gameObject.transform.Find("Particle_Foreground").GetComponent<ParticleSystem>();
		component2.Play();
	}

	private IEnumerator PlayAllAnimation()
	{
		if (showStar1)
		{
			yield return new WaitForSeconds(timeBeforeStar1Animation);
			yield return StartCoroutine(PlayAnimationStar(starFillMaxHeight, star1Fill, speedStar1Animation, star1MinSize, star1MaxSize, "star_1"));
		}
		if (showStar2)
		{
			yield return new WaitForSeconds(timeBeforeStar2Animation);
			yield return StartCoroutine(PlayAnimationStar(starFillMaxHeight, star2Fill, speedStar2Animation, star2MinSize, star2MaxSize, "star_2"));
		}
		if (showStar3)
		{
			yield return new WaitForSeconds(timeBeforeStar3Animation);
			yield return StartCoroutine(PlayAnimationStar(starFillMaxHeight, star3Fill, speedStar3Animation, star3MinSize, star3MaxSize, "star_3"));
		}
		if (showCrown)
		{
			yield return new WaitForSeconds(timeBeforeCrownAnmation);
			yield return StartCoroutine(PlayAnimationCrown());
		}
	}

	private IEnumerator PlayAnimationCrown()
	{
		Image shine = crownFill.parent.FindDeepChild("Crown_Shine").GetComponent<Image>();
		PlayStarPartciles(crownFill.parent);
		AudioController.instance.Play("crown_in_complete_room");
		float progress = 0f;
		while (progress < 1f)
		{
			progress = Mathf.Min(1f, progress + crownSpeedAlpha);
			Image image = crownFillImage;
			Color color = crownFillImage.color;
			float r = color.r;
			Color color2 = crownFillImage.color;
			float g = color2.g;
			Color color3 = crownFillImage.color;
			image.color = new Color(r, g, color3.b, progress);
			Image image2 = shine;
			Color color4 = shine.color;
			float r2 = color4.r;
			Color color5 = shine.color;
			float g2 = color5.g;
			Color color6 = shine.color;
			image2.color = new Color(r2, g2, color6.b, progress);
			yield return new WaitForEndOfFrame();
		}
		while (progress > 0f)
		{
			progress = Mathf.Max(0f, progress - crownSpeedAlpha);
			Image image3 = shine;
			Color color7 = shine.color;
			float r3 = color7.r;
			Color color8 = shine.color;
			float g3 = color8.g;
			Color color9 = shine.color;
			image3.color = new Color(r3, g3, color9.b, progress);
			yield return new WaitForEndOfFrame();
		}
	}

	private IEnumerator PlayAnimationStar(float maxHeight, RectTransform trans, float speedFill, float minSize, float maxSize, string starAudio)
	{
		Image fill2 = trans.parent.transform.FindDeepChild("Star_Fill2").GetComponent<Image>();
		Image shine = trans.parent.transform.FindDeepChild("Star_Shine").GetComponent<Image>();
		float progress2 = 0f;
		AudioController.instance.Play("star_loading_sound");
		while (progress2 < 1f)
		{
			progress2 = Mathf.Min(1f, progress2 + speedFill);
			SetProgress(progress2, maxHeight, trans);
			yield return new WaitForEndOfFrame();
		}
		PlayStarPartciles(trans.parent);
		AudioController.instance.Play(starAudio);
		progress2 = 0f;
		while (progress2 < 1f)
		{
			progress2 = Mathf.Min(1f, progress2 + speedResizeStar);
			Image image = fill2;
			Color color = fill2.color;
			float r = color.r;
			Color color2 = fill2.color;
			float g = color2.g;
			Color color3 = fill2.color;
			image.color = new Color(r, g, color3.b, progress2);
			Image image2 = shine;
			Color color4 = shine.color;
			float r2 = color4.r;
			Color color5 = shine.color;
			float g2 = color5.g;
			Color color6 = shine.color;
			image2.color = new Color(r2, g2, color6.b, progress2);
			float newValue = minSize + progress2 * (maxSize - minSize);
			trans.parent.localScale = new Vector3(newValue, newValue);
			yield return new WaitForEndOfFrame();
		}
		while (progress2 > 0f)
		{
			progress2 = Mathf.Max(0f, progress2 - speedResizeStar);
			Image image3 = shine;
			Color color7 = shine.color;
			float r3 = color7.r;
			Color color8 = shine.color;
			float g3 = color8.g;
			Color color9 = shine.color;
			image3.color = new Color(r3, g3, color9.b, progress2);
			float newValue2 = minSize + progress2 * (maxSize - minSize);
			trans.parent.localScale = new Vector3(newValue2, newValue2);
			yield return new WaitForEndOfFrame();
		}
	}

	private void SetProgress(float progressValue, float maxHeight, RectTransform trans)
	{
		float y = maxHeight * progressValue;
		Vector2 sizeDelta = trans.sizeDelta;
		trans.sizeDelta = new Vector3(sizeDelta.x, y);
	}

	public void OnShowEnd()
	{
		StartCoroutine(PlayAllAnimation());
	}

	public void OnEndHide()
	{
		AudioController.instance.UnPauseBackground();
		UnityEngine.Object.Destroy(base.gameObject);
	}

	public void SetStarsCrownAndDrink(int howManyStars, bool crown, GameObject glassFilledPrefab)
	{
		switch (howManyStars)
		{
		case 1:
			showStar1 = true;
			break;
		case 2:
			showStar1 = true;
			showStar2 = true;
			break;
		case 3:
			showStar1 = true;
			showStar2 = true;
			showStar3 = true;
			break;
		}
		showCrown = crown;
		if (glassFilledPrefab != null)
		{
			CreateDrink(glassFilledPrefab);
		}
	}

	private void CreateDrink(GameObject glassFilledPrefab)
	{
		instanceDrink = UnityEngine.Object.Instantiate(glassFilledPrefab);
		Liquid[] componentsInChildren = glassFilledPrefab.GetComponentsInChildren<Liquid>();
		Liquid[] componentsInChildren2 = instanceDrink.GetComponentsInChildren<Liquid>();
		LiquidColorData[] array = new LiquidColorData[componentsInChildren2.Length];
		Vector2[] array2 = new Vector2[componentsInChildren2.Length];
		for (int i = 0; i < componentsInChildren2.Length; i++)
		{
			componentsInChildren2[i].SetRawHeight(componentsInChildren[i].GetRawHeight());
			array[i] = componentsInChildren[i].GetColorData();
			array2[i] = new Vector2(componentsInChildren[i].GetRawWidth(), componentsInChildren[i].GetRawHeight());
			UnityEngine.Object.Destroy(componentsInChildren2[i]);
		}
		int num = 400;
		Transform transform = instanceDrink.transform;
		Vector3 position = instanceDrink.transform.position;
		float x = position.x;
		Vector3 position2 = instanceDrink.transform.position;
		transform.position = new Vector3(x, position2.y, -9f);
		SpriteRenderer[] componentsInChildren3 = instanceDrink.GetComponentsInChildren<SpriteRenderer>();
		for (int j = 0; j < componentsInChildren3.Length; j++)
		{
			switch (componentsInChildren3[j].gameObject.name)
			{
			case "BrighterIllumination":
				componentsInChildren3[j].sortingOrder = 401;
				break;
			case "BrightestIllumination":
				componentsInChildren3[j].sortingOrder = 404;
				break;
			default:
				componentsInChildren3[j].sortingOrder += num;
				break;
			}
		}
		SpriteMask[] componentsInChildren4 = instanceDrink.GetComponentsInChildren<SpriteMask>();
		for (int k = 0; k < componentsInChildren4.Length; k++)
		{
			switch (componentsInChildren4[k].gameObject.name)
			{
			case "BrighterLiquidMask":
				componentsInChildren4[k].frontSortingOrder = 401;
				componentsInChildren4[k].backSortingOrder = 400;
				break;
			case "BrightestLiquidMask":
				componentsInChildren4[k].frontSortingOrder = 404;
				componentsInChildren4[k].backSortingOrder = 403;
				break;
			default:
				componentsInChildren4[k].frontSortingOrder += num;
				componentsInChildren4[k].backSortingOrder += num;
				break;
			}
		}
		instanceDrink.transform.FindDeepChild("TargetLines").gameObject.SetActive(value: false);
		MeshRenderer[] componentsInChildren5 = instanceDrink.GetComponentsInChildren<MeshRenderer>();
		for (int l = 0; l < componentsInChildren5.Length; l++)
		{
			componentsInChildren5[l].transform.FindDeepChild("BrighterIllumination").GetComponent<SpriteRenderer>().color = array[l].glassLiquidBrighter;
			componentsInChildren5[l].transform.FindDeepChild("BrightestIllumination").GetComponent<SpriteRenderer>().color = array[l].glassLiquidBrightest;
		}
		GameObject[] array3 = new GameObject[componentsInChildren5.Length];
		for (int m = 0; m < componentsInChildren5.Length; m++)
		{
			GameObject gameObject = componentsInChildren5[m].gameObject;
			array3[m] = new GameObject("LiquidBase");
			array3[m].transform.parent = gameObject.transform;
			Transform transform2 = array3[m].transform;
			float y = array2[m].y / 2f;
			Vector3 localPosition = array3[m].transform.localPosition;
			transform2.localPosition = new Vector3(0f, y, localPosition.z);
			UnityEngine.Object.Destroy(componentsInChildren5[m]);
			UnityEngine.Object.Destroy(componentsInChildren5[m].gameObject.GetComponent<MeshFilter>());
			SpriteRenderer spriteRenderer = array3[m].AddComponent<SpriteRenderer>();
			spriteRenderer.sprite = Resources.Load<Sprite>("Images/white_bg");
			spriteRenderer.drawMode = SpriteDrawMode.Tiled;
			spriteRenderer.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;
			spriteRenderer.sortingOrder = num;
			spriteRenderer.color = array[m].glassLiquidBase;
			spriteRenderer.size = array2[m];
		}
		instanceDrink.transform.SetParent(glassPosition);
		instanceDrink.transform.localScale = new Vector3(drinkScale, drinkScale, drinkScale);
		Transform transform3 = instanceDrink.transform;
		Vector3 localPosition2 = instanceDrink.transform.localPosition;
		transform3.localPosition = new Vector3(0f, 0f, localPosition2.z);
	}

	public void OnClickRestart()
	{
		AudioController.instance.Play("button");
		GetComponent<Animator>().SetTrigger("Hide");
		Object.FindObjectOfType<GameController>().OnRestartLevel();
	}

	public void OnClickMenu()
	{
		AudioController.instance.Play("button");
		GetComponent<Animator>().SetTrigger("Hide");
		Object.FindObjectOfType<GameController>().BackToMainMenu(goToCurrentLevel: true);
	}

	public void OnClickNextLevel()
	{
		AudioController.instance.Play("button");
		GetComponent<Animator>().SetTrigger("Hide");
		Object.FindObjectOfType<GameController>().OnNextLevel();
	}
}
